module App::View
    class ItemForm < Qt::Widget
        slots 'calcDurationClicked()'
        slots 'calcStopClicked()'

        attr_reader :line, :billable, :start, :stop, :buttonbox

        def initialize(type = App::Items::Task)
            super()

            layout = Qt::VBoxLayout.new
            @line = Qt::LineEdit.new
            layout.addWidget @line
            if type == App::Items::Client
            elsif type == App::Items::Project
                @billable = Qt::CheckBox.new "Billable?"
                layout.addWidget @billable
            elsif type == App::Items::Task
                grid = Qt::GridLayout.new

                grid.addWidget Qt::Label.new("start:"), 0, 0
                if @simplified_times
                    @start = Qt::TimeEdit.new
                    @start.setDisplayFormat "HH:mm"
                else
                    @start = Qt::DateTimeEdit.new
                    @start.setDisplayFormat "yy/MM/dd dddd HH:mm"
                end
                grid.addWidget @start, 0, 1

                grid.addWidget Qt::Label.new("stop:"), 1, 0
                if @simplified_times
                    @stop = Qt::TimeEdit.new
                    @stop.setDisplayFormat "HH:mm"
                else
                    @stop = Qt::DateTimeEdit.new
                    @stop.setDisplayFormat "yy/MM/dd dddd HH:mm"
                end
                grid.addWidget @stop, 1, 1
                calc1 = Qt::Label.new("<a href=\"#\">calculate</a>")
                connect calc1, SIGNAL('linkActivated(const QString)'),
                          self, SLOT('calcStopClicked()')
                grid.addWidget calc1, 1, 2

                grid.addWidget Qt::Label.new("duration:"), 2, 0
                hbox = Qt::HBoxLayout.new
                @spinbox_hour_duration = Qt::SpinBox.new
                @spinbox_hour_duration.setMinimum 0
                @spinbox_hour_duration.setSuffix " hour(s)"
                hbox.addWidget @spinbox_hour_duration
                @spinbox_min_duration = Qt::SpinBox.new
                @spinbox_min_duration.setMinimum 0
                @spinbox_min_duration.setMaximum 59
                @spinbox_min_duration.setSuffix " min(s)"
                hbox.addWidget @spinbox_min_duration
                @spinbox_sec_duration = Qt::SpinBox.new
                @spinbox_sec_duration.setMinimum 0
                @spinbox_sec_duration.setMaximum 59
                @spinbox_sec_duration.setSuffix " sec(s)"
                hbox.addWidget @spinbox_sec_duration unless @simplified_times
                grid.addLayout hbox, 2, 1
                @calc2 = Qt::Label.new("<a href=\"#\">calculate</a>")
                connect @calc2, SIGNAL('linkActivated(const QString)'),
                          self, SLOT('calcDurationClicked()')
                grid.addWidget @calc2, 2, 2

                layout.addLayout grid
            end

            @buttonbox = Qt::DialogButtonBox.new
            @buttonbox.addButton Qt::DialogButtonBox::Ok
            @buttonbox.addButton Qt::DialogButtonBox::Cancel
            layout.addWidget @buttonbox

            setLayout layout

            p = mapToGlobal($widget.view.pos)
            setGeometry(Qt::Rect.new(p.x - 50, p.y + 50, minimumWidth, minimumHeight))
        end

        def calcStopClicked
            startobj = stopobj = nil
            if @simplified_times
                startobj = @start.time
                stopobj = @stop.time
            else
                startobj = @start.dateTime
                stopobj = @stop.dateTime
            end
            duration = startobj.secsTo stopobj
            if duration < 0
                if @simplified_times
                    d1 = startobj.secsTo(Qt::Time.new(23, 59, 59)) + 1
                    d2 = Qt::Time.new(00, 00).secsTo stopobj
                    duration = d1 + d2
                else
                    puts "stop less than start!"
                end
            end
            @spinbox_hour_duration.setValue duration/60/60
            @spinbox_min_duration.setValue duration/60%60
            @spinbox_sec_duration.setValue duration%60
        end
        def calcDurationClicked
            if @simplified_times
                startobj = @start.time
            else
                startobj = @start.dateTime
            end
            stopobj = startobj.addSecs(
                @spinbox_hour_duration.value*60*60 + @spinbox_min_duration.value*60 +
                @spinbox_sec_duration.value)
            if @simplified_times
                @stop.setTime stopobj
            else
                @stop.setDateTime stopobj
            end
        end

        def getDuration
            @spinbox_hour_duration.value*60*60 + @spinbox_min_duration.value*60 +
              @spinbox_sec_duration.value
        end
    end
end
